package com.lening.controller;

import com.lening.entity.*;
import com.lening.service.StuService;
import com.lening.utils.ExportExcel;
import com.lening.utils.ExportPdf;
import com.lening.utils.FileUtils;
import com.lening.utils.MyReadXlx;
import net.sf.json.JSONArray;
import org.apache.ibatis.annotations.Param;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.*;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;

/**
 * 创作时间：2019/8/22 9:45
 * 作者：李增强
 */
@Controller
public class StuController {
    @Resource
    StuService stuService;

    @RequestMapping("/getStuPDF")
    public void getStuPDF(HttpServletResponse response){
        List<StuBean> list = stuService.getStuList();
        OutputStream out =null;
        try {
            out = response.getOutputStream();
            response.reset(); // 重点突出
            response.setCharacterEncoding("UTF-8"); // 重点突出
            response.setContentType("application/x-msdownload");// 不同类型的文件对应不同的MIME类型 // 重点突出
            // inline在浏览器中直接显示，不提示用户下载
            // attachment弹出对话框，提示用户进行下载保存本地
            // 默认为inline方式
            String fileName = new Date()+".pdf";
            response.setHeader("Content-Disposition", "attachment;filename="+fileName);
            //  response.setHeader("Content-Disposition", "attachment; filename="+fileName); // 重点突出
            //设置表头
            String[] headers = { "用户ID", "用户名", "地址", "班级"};

            //创建一个工作薄
            ExportPdf<StuBean> ex = new ExportPdf<StuBean>();
            ex.exportPdf(headers, list, out);
            //FileUtils.downloadFile(request, response, work, "user-all.pdf");
            JOptionPane.showMessageDialog(null, "pdf导出成功!");
            System.out.println("pdf导出成功！");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


    @RequestMapping("/saveExcelImport")
    public String saveExcelImport(HttpServletRequest request, @Param("filename")MultipartFile filename){

       //File file1 = new File("E:/test/aaa.xls");

        try {
            //InputStream is = new FileInputStream(file1);
            List<StuBean> list = MyReadXlx.readXls(filename.getInputStream());
            stuService.saveExcel(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:getStuList.do";
    }

    @RequestMapping("/toExcelImport")
    public  String toExcelImport(){
        return "excel_import";
    }
    @RequestMapping("/getStuExcel")
    public void getStuExcel(HttpServletRequest request,HttpServletResponse response){
        /**
         * 把要导出的内容查询出来
         */
        List<StuBean> list = stuService.getStuList();
        /**
         * 把这个list装进excel里面
         */
        // 创建一个工作薄
        HSSFWorkbook work = new HSSFWorkbook();
        ExportExcel.getExportExcel(work, list);

        /**
         * 把excel最为文件提供给用户下载
         */
        FileUtils.downloadFile(request, response, work, "这是测试的导出.xls");
        System.out.println(list);
    }

    /**
     * 查看班级信息
     */
    @RequestMapping("/getGradeByGid")
    public ModelAndView getGradeByGid(Integer gid){
        ModelAndView mv = new ModelAndView("grade_info");
        GradeBean gb = stuService.getGradeByGid(gid);
        mv.addObject("gb", gb);
        return mv;
    }

    /**
     * 更新班级讲师
     */
    @RequestMapping("/updateGradeTeacher")
    public String updateGradeTeacher(Integer gid,Integer tid){
        stuService.updateGradeTeacher(gid,tid);
        return "redirect:getGradeList.do";
    }


    /**
     * 根据讲师id获取讲师所有班级信息（查询讲师信息）
     */
    @RequestMapping("/getTeacherByTid")
    public void getTeacherByTid(Integer tid, HttpServletResponse response){
        TeacherBean tb = stuService.getTeacherByTid(tid);
        String count = "0";
        if(tb.getGbs()!=null){
           count = tb.getGbs().size()+"";
        }
        try {
            response.setCharacterEncoding("UTF-8");
            response.getWriter().write(count);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 去给班级分配讲师
     */
    @RequestMapping("/toGradeTeacher")
    public String toGradeTeacher(Integer gid,Model model){
        GradeBean gb = stuService.getGradeByGid(gid);
        List<TeacherBean> list = stuService.getTeacherList();
        model.addAttribute("gb", gb);
        model.addAttribute("list", list);
        return "grade_teacher";
    }


    @RequestMapping("/updateGradeRoom")
    public String updateGradeRoom(Integer gid,Integer rid){
        stuService.updateGradeRoom(gid,rid);
        return "redirect:getGradeList.do";
    }

    /**
     * 去给班级分配教室
     * 两种方式，都可以使用，看个人习惯
     */
    @RequestMapping("/toGradeRoom")
    public String toGradeRoom(Integer gid, Model model){
        List<RoomBean> list = stuService.getRoomList();
        GradeBean gb = stuService.getGradeByGid(gid);
        model.addAttribute("list", list);
        model.addAttribute("gb", gb);
        return "grade_room";
    }
    /**
     * 获取教室列表
     * @return
     */
    @RequestMapping("/getRoomList")
    public ModelAndView getRoomList(){
        ModelAndView mv = new ModelAndView("room_list");
        List<RoomBean> list = stuService.getRoomList();
        mv.addObject("list", list);
        return mv;
    }

    @RequestMapping("/getTeacherList")
    public ModelAndView getTeacherList(){
        ModelAndView mv = new ModelAndView("teacher_list");
        List<TeacherBean> list = stuService.getTeacherList();
        mv.addObject("list", list);
        return mv;
    }

    @RequestMapping("/getGradeList")
    public ModelAndView getGradeList(){
        ModelAndView mv = new ModelAndView("grade_list");
        List<GradeBean> list = stuService.getGradeList();
        mv.addObject("list", list);
        return mv;
    }
    /**
     * 修改学生班级
     */
    @RequestMapping("/updateStuGrade")
    public String updateStuGrade(Integer id,Integer gid){
        stuService.updateStuGrade(id,gid);
        return "redirect:getStuList.do";
    }


    /**
     * 去给学生选择班级
     */
    @RequestMapping("/toStuGrade")
    public  ModelAndView toStuGrade(Integer id){
        ModelAndView mv = new ModelAndView("stu_grade");
        List<GradeBean> list = stuService.getGradeList();
        StuBean sb = stuService.getStuById(id);
        mv.addObject("list", list);
        mv.addObject("sb", sb);
        return mv;
    }

    /**
     * 获取学生列表
     * @return
     */
    @RequestMapping("/getStuList")
    public ModelAndView getStuList(){
        ModelAndView mv = new ModelAndView("stu_list");
        List<StuBean> list = stuService.getStuList();
        mv.addObject("list", list);
        return mv;
    }

    @RequestMapping("/getPowerJson")
    public ModelAndView getPowerJson(){
        ModelAndView mv = new ModelAndView("left");
        List<PowerBean> list = stuService.getPowerList();
        String json = JSONArray.fromObject(list).toString();
        mv.addObject("json", json);
        return mv;
    }

}
